package service

import (
	"errors"
	uuid "github.com/satori/go.uuid"
	"go_shengshu_admin/model"
	"go_shengshu_admin/repository"
)

type CategoryService struct {
	Repo *repository.CategoryRepository
}

type CategorySrv interface {
	List() (categorys []*model.CategoryResult, err error)
	GetTotal() (total int64, err error)
	Get(id string) (categorys *model.CategoryResult, err error)
	Exist(category model.Category) *model.Category
	ExistByCategoryId(id string) *model.Category
	Add(category model.Category) (bool, error)
	Edit(category model.Category) (bool, error)
	Delete(id string) (bool, error)
}

func (srv *CategoryService) List() (categorys []*model.CategoryResult, err error) {
	return srv.Repo.List()
}

func (srv *CategoryService) GetTotal() (total int64, err error) {
	return srv.Repo.GetTotal()
}

func (srv *CategoryService) Get(id string) (categorys *model.CategoryResult, err error) {
	return srv.Repo.Get(id)
}

func (srv *CategoryService) Exist(category model.Category) *model.Category {
	return srv.Repo.Exist(category)
}

func (srv *CategoryService) ExistByCategoryId(id string) *model.Category {
	return srv.Repo.ExistByCategoryId(id)
}

func (srv *CategoryService) Add(category model.Category) (bool, error) {
	if category.Name == "" {
		return false, errors.New("参数错误")
	}
	if category.CategoryId==""{
		category.CategoryId = uuid.NewV4().String()
	}
	category.IsDeleted = false
	return srv.Repo.Add(category)
}

func (srv *CategoryService) Edit(category model.Category) (bool, error) {
	if category.Name == "" || category.CategoryId == "" {
		return false, errors.New("参数错误")
	}

	if srv.Repo.ExistByCategoryId(category.CategoryId) != nil {
		return false, errors.New("目录不存在")
	}
	return srv.Repo.Edit(category)
}

func (srv *CategoryService) Delete(id string) (bool, error) {
	if id == "" {
		return false, errors.New("参数错误")
	}

	if srv.Repo.ExistByCategoryId(id) != nil {
		return false, errors.New("目录不存在")
	}
	return srv.Repo.Delete(id)
}
